﻿@using MvcContrib.UI.Grid
@model XRM.Security.WebUI.Models.SystemUser.ADUserDialogModel
@{
    ViewBag.Title = "Выбор пользователя";
    Layout = "~/Views/Shared/_LayoutDialog.cshtml";
}

<style type="text/css">
    #table_dialog_id tbody tr
    {
        cursor: pointer;
    }
    #table_dialog_id .selected td {
        background-color: #428bca;
        color: white;
    }
</style>

<div class="modal-body" style="height:400px;">
    @using (Html.BeginForm("ADUsersDialog", "CertRegister", FormMethod.Post, new { id = "dlgADUsersDialogForm", @class = "" }))
    {      
        @Html.AntiForgeryToken()
        @Html.HiddenFor(m => m.SubmitAction)
        @Html.HiddenFor(m => m.SelectedFullName)
        @Html.HiddenFor(m => m.SelectedLogin)
                 
    
        if(Model.ADUserList.Any())
        {
            Html.Grid(Model.ADUserList).Columns(column =>
                {
                    column.For(item => item.FullName).Named("Имя пользователя");
                    column.For(item => item.AccountName).Named("Логин");
                }).Attributes(@class => "table table-striped table-bordered table-hover", id => "table_dialog_id").Render();
        }
    
<div id="divInfo" class="form-horizontal form-horizontal-2">
    <div class="form-group" style="display:block;margin-bottom:15px;">
        @Html.LabelFor(m => m.DepartmentId, new { @class = "control-label" })
        <div class="col-sm-6">
            @Html.DropDownListFor(m => m.DepartmentId, new SelectList(Model.DepartmentAllList, "Id", "Name"), new { @class = "form-control", style="display:block;width:100%;" })
            @Html.ValidationMessageFor(m => m.DepartmentId)
        </div>
    </div>
        
    <div class="form-group"  style="display:block;">
        @Html.LabelFor(m => m.DepartmentAddressId, new { @class = "control-label" })
        <div class="col-sm-6">
            @Html.DropDownListFor(m => m.DepartmentAddressId, new SelectList(Model.DepartmentAddressAllList, "Id", "Address"), new { @class = "form-control", style="display:block;width:100%;" })
            @Html.ValidationMessageFor(m => m.DepartmentAddressId)
        </div>
    </div>
</div>
}
</div>

<div class="modal-footer">
    <button type="button" class="btn btn-primary" id="btnChoise">Выбрать</button>
    <button type="button" class="btn btn-default" data-dismiss="modal">Отмена</button>
</div>
    
@Scripts.Render("~/bundles/gridlibrary")
<script type="text/javascript" src="@(Url.Content("~/Scripts/AjaxRequest.js"))"></script>    
<script type="text/javascript">
    $(document).ready(function () {
        $('#DepartmentId').change(function () {
            GetDepartmentAddress();
        });
        IniDepartment();

        $('#btnChoise').on('click', function () { ChoiseUser(); });
        $('#btnChoise').prop('disabled', true);

        @if (Model.ADUserList.Any())
        {
            <text>
                $('#table_dialog_id').dataTable({
                    paging: true,
                    bFilter: false,
                    bSort: false,
                    info: false
            
                });
                $('#table_dialog_id tbody').on('click', 'tr', function () {
                    if ($(this).attr('id'))
                        if ($(this).attr('id') == "fakeRow")
                            return;

                    if ($(this).hasClass('selected')) {
                        $(this).removeClass('selected');
                        $('#btnChoise').prop('disabled', true);
                    }
                    else {
                        $('#table_dialog_id').dataTable().$('tr.selected').removeClass('selected');
                        $(this).addClass('selected');
                        $('#btnChoise').prop('disabled', false);
                        var fr = $('#fakeRow');
                        if (fr.length == 0)
                            fr = GetFakeRow();
                        else
                            fr = fr.detach();
                        $(this).after(fr);
                        IniDepartment();
                    }
                });

                $('#table_dialog_id tbody tr:first').trigger("click");
            </text>
        }
    });

    function IniDepartment() {
        $('#DepartmentId').val("-1");
        $('#DepartmentAddressId').prop('disabled', true);
    }

    function GetFakeRow() {
        var td = $("<td colspan='2'></td>");
        td.append($("#divInfo"));
        var row = $("<tr id='fakeRow'></tr>");
        row.append(td);

        return row;
    }

    function ChoiseUser() {
        var trSelected = $('#table_dialog_id').dataTable().$('tr.selected');
        if (trSelected) {
            $('#SubmitAction').val("choise");
            $('#SelectedFullName').val(trSelected.find("td").eq(0).html());
            $('#SelectedLogin').val(trSelected.find("td").eq(1).html());
            $("#dlgADUsersDialogForm").submit();
        }
        else {
            alert('Нет выбранных пользователей!');
        }
    }
</script>
